﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace YTCMS.Core
{
    public static class AppPagerHelper
    {
        private const string REQUEST_PAGEINDEX_NAME = "pageIndex";
        private const int PAGEER_COUNT = 5;
        private static string formatPager = string.Empty;
        private static string gotoPager = string.Empty;

        public static MvcHtmlString AjaxPager1(this HtmlHelper html, int pageSize, int totalCount)
        {
            formatPager = "javascript:to_page({0});";
            gotoPager = "javascript:gotopage();";
            return GeneratePager1(html, pageSize, totalCount);
        }
        public static MvcHtmlString ActionAjaxPager1(this HtmlHelper html, int pageSize, int totalCount, string actionName)
        {
            formatPager = "javascript:actionto_page({0},[1]);";
            gotoPager = "javascript:actiongotopage();";
            MvcHtmlString mvch = GeneratePager1(html, pageSize, totalCount);
            string str = (mvch == null ? "" : mvch.ToString().Replace("[1]", "\"" + actionName + "\""));
            return MvcHtmlString.Create(str);
        }

        public static MvcHtmlString AjaxPager1(this HtmlHelper html, dynamic pageSize, dynamic totalCount)
        {
            formatPager = "javascript:to_page({0});";
            gotoPager = "javascript:gotopage();";
            return GeneratePager1(html, pageSize, totalCount);
        }

        public static MvcHtmlString AjaxPager1(this HtmlHelper html, int pageSize, int totalCount, string style, bool isStyle)
        {
            formatPager = "javascript:to_page({0});";
            gotoPager = "javascript:gotopage();";
            return GeneratePager1(html, pageSize, totalCount, style);
        }


        public static MvcHtmlString AjaxPager1(this HtmlHelper html, int pageSize, int totalCount, string otherParams)
        {
            if (!string.IsNullOrEmpty(otherParams))
            {
                gotoPager = "javascript:gotopage(\"" + otherParams + "\");";
                formatPager = "javascript:to_pageAndparams({0},\"" + otherParams + "\");";
            }
            else
            {
                gotoPager = "javascript:gotopage();";
                formatPager = "javascript:to_page({0});";
            }

            return GeneratePager1(html, pageSize, totalCount);
        }

        public static MvcHtmlString AjaxPager1(this HtmlHelper html, int pageSize, int totalCount, bool b, string target = "searchBtn", string dataType = "text", string type = "post")
        {
            //to_page({ target: "searchBtn",dataType: "text",type: "post",cache: false,isError: false,pageIndex: 1,pageSize: 10,})
            formatPager = "javascript:to_CommonPage({0},{1},\"{2}\",\"{3}\",\"{4}\");";
            gotoPager = "javascript:gotopage();";
            return GenerateCommonPager1(html, pageSize, totalCount, target, dataType, type);
        }

        public static MvcHtmlString NormalPager1(this HtmlHelper html, int pageSize, int totalCount)
        {
            formatPager = "?pageIndex={0}";

            return GeneratePager1(html, pageSize, totalCount);
        }

        public static MvcHtmlString NormalPager1(this HtmlHelper html, int pageSize, int totalCount, string otherParams)
        {
            if (!string.IsNullOrEmpty(otherParams))
            {
                formatPager = "?pageIndex={0}&" + otherParams + "";
            }
            else
            {
                formatPager = "?pageIndex={0}";
            }

            return GeneratePager1(html, pageSize, totalCount);
        }

        private static MvcHtmlString GeneratePager1(HtmlHelper html, int pageSize, int totalCount, int totalPagesCount)
        {
            if (totalPagesCount <= 1) return null;
            var currentPageIndex = GetCurrentPage(html, totalPagesCount);
            var output = new StringBuilder();
            output.Append("<div class=\"xans-element- xans-product xans-product-normalpaging\">");
            //HandleTotalSumary(output, totalCount, totalPagesCount);
            HandleFirstPage(output, currentPageIndex);
            HandlePreviousPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            HandleNextPage(output, totalPagesCount, currentPageIndex);
            HandleLastPage(output, totalPagesCount, currentPageIndex);
            //output.Append("<div class=\"clearfix\"> </div>");
            output.Append("</div>");
            return new MvcHtmlString(output.ToString());
        }

        private static MvcHtmlString GeneratePager(int pageIndex, int pageSize, int totalCount, int totalPagesCount)
        {
            if (totalPagesCount <= 1) return null;
            var currentPageIndex = pageIndex;
            var output = new StringBuilder();
            output.Append("<div class=\"dataTables_paginate\" >");
            //HandleTotalSumary(output, totalCount, totalPagesCount);
            HandlePreviousPage(output, currentPageIndex);
            HandleFirstPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            HandleLastPage(output, totalPagesCount, currentPageIndex);
            HandleNextPage(output, totalPagesCount, currentPageIndex);
            output.Append("</div>");
            return new MvcHtmlString(output.ToString());
        }

        private static MvcHtmlString GeneratePager1(HtmlHelper html, string style, int pageSize, int totalCount, int totalPagesCount)
        {
            if (totalPagesCount <= 1) return null;
            var currentPageIndex = GetCurrentPage(html, totalPagesCount);
            var output = new StringBuilder();
            output.Append(string.Format("<div class=\"{0}\" >", style));
            HandlePreviousPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            HandleNextPage(output, totalPagesCount, currentPageIndex);
            output.Append("</div>");
            return new MvcHtmlString(output.ToString());
        }

        private static MvcHtmlString GeneratePager1(HtmlHelper html, int pageSize, int totalCount)
        {
            var totalPagesCount = Math.Max((totalCount + pageSize - 1) / pageSize, 1);
            if (totalPagesCount <= 1) return null;

            var currentPageIndex = GetCurrentPage(html, totalPagesCount);

            var output = new StringBuilder();
            output.Append("<div class=\"xans-element- xans-product xans-product-normalpaging\">");

            //HandleTotalSumary(output,totalCount,pageSize);
            // HandlePreviousPage(output, currentPageIndex);
            HandleFirstPage(output, currentPageIndex);
            output.Append("<ol>");
            //HandleFirstPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            output.Append("</ol>");
            HandleLastPage(output, totalPagesCount, currentPageIndex);
            //HandleNextPage(output, totalPagesCount, currentPageIndex);
            //output.Append("<div class=\"clearfix\"> </div>");
            output.Append("</div>");

            return new MvcHtmlString(output.ToString());
        }

        private static MvcHtmlString GenerateCommonPager1(HtmlHelper html, int pageSize, int totalCount, string target, string dataType, string type)
        {
            var totalPagesCount = Math.Max((totalCount + pageSize - 1) / pageSize, 1);
            if (totalPagesCount <= 1) return null;

            var currentPageIndex = GetCurrentPage(html, totalPagesCount);

            var output = new StringBuilder();
            output.Append("<div class=\"dataTables_paginate\" >");
            HandleFirstCommonPage(output, currentPageIndex, pageSize, target, dataType, type);
            HandlePreviousCommonPage(output, currentPageIndex, pageSize, target, dataType, type);
            HandleCommonPagers(output, totalPagesCount, currentPageIndex, pageSize, target, dataType, type);
            HandleNextCommonPage(output, totalPagesCount, currentPageIndex, pageSize, target, dataType, type);
            HandleLastCommonPage(output, totalPagesCount, currentPageIndex, pageSize, target, dataType, type);
            output.Append("</div>");

            return new MvcHtmlString(output.ToString());
        }

        private static MvcHtmlString GeneratePager1(HtmlHelper html, int pageSize, int totalCount, string style)
        {
            var totalPagesCount = Math.Max((totalCount + pageSize - 1) / pageSize, 1);
            if (totalPagesCount <= 1) return null;

            var currentPageIndex = GetCurrentPage(html, totalPagesCount);

            var output = new StringBuilder();
            output.Append("<div class=\"" + style + "\" >");

            HandlePreviousPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            HandleNextPage(output, totalPagesCount, currentPageIndex);

            output.Append("</div>");

            return new MvcHtmlString(output.ToString());
        }



        private static void HandleFirstPage(StringBuilder output, int currentPageIndex)
        {
            if (currentPageIndex == 1)
            {
                output.Append(GetATag("首页", "javascript:void(0)", ""));
            }
            else
            {
                output.Append(GetATag("首页", GetAHref(1), ""));
            }
        }
        private static void HandleFirstCommonPage(StringBuilder output, int currentPageIndex, int pageSize, string target, string dataType, string type)
        {
            if (currentPageIndex == 1)
            {
                output.Append(GetATag("首页", "javascript:void(0)", "first paginate_button"));
            }
            else
            {
                output.Append(GetATag("首页", GetCommonAHref(1, pageSize, target, dataType, type), "first paginate_button"));
            }
        }

        private static void HandlePreviousPage(StringBuilder output, int currentPageIndex)
        {
            if (currentPageIndex == 1)
            {
                output.Append(GetATag("<span>&#8592;</span>", "javascript:void(0)", "prev"));
            }
            else
            {
                output.Append(GetATag("<span>&#8592;</span>", GetAHref(currentPageIndex - 1), "prev"));
            }
        }

        private static void HandlePreviousCommonPage(StringBuilder output, int currentPageIndex, int pageSize, string target, string dataType, string type)
        {
            if (currentPageIndex == 1)
            {
                output.Append(GetATag("上一页", "javascript:void(0)", "previous paginate_button"));
            }
            else
            {
                output.Append(GetATag("上一页", GetCommonAHref(currentPageIndex - 1, pageSize, target, dataType, type), "previous paginate_button"));
            }
        }

        private static void HandlePagers(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            int startIndex = GetStartIndex(totalPagesCount, currentPageIndex);

            for (var i = 0; i < PAGEER_COUNT; i++)
            {
                int pageIndex = startIndex + i;

                if (pageIndex > totalPagesCount) break;
                if (currentPageIndex == pageIndex)
                {
                    output.Append(GetATag(pageIndex,"javascript:void(0)", "curent"));
                }
                else
                {
                    output.Append(GetATag(pageIndex, GetAHref(pageIndex), ""));
                }

            }
            ////if(_showSpan)
            ////{
            ////    if (totalPagesCount - currentPageIndex >=2)
            ////    {
            ////        output.Append("<span>...</span>");

            ////    }
            ////    output.Append("<cite>到第<input id='toPage' maxlength='5' name='toPage' type='text'/>页</cite>");
            ////    output.Append(GetATag("确定", gotoPager));
            ////    //output.Append("<a href='javascript:gotoPage();'>确定</a>");
            ////}
        }

        private static void HandleCommonPagers(StringBuilder output, int totalPagesCount, int currentPageIndex, int pageSize, string target, string dataType, string type)
        {
            int startIndex = GetStartIndex(totalPagesCount, currentPageIndex);

            for (var i = 0; i < PAGEER_COUNT; i++)
            {
                int pageIndex = startIndex + i;

                if (pageIndex > totalPagesCount) break;
                if (currentPageIndex == pageIndex)
                {
                    output.Append(GetATag(pageIndex, GetCommonAHref(pageIndex, pageSize, target, dataType, type), "paginate_active"));
                }
                else
                {
                    output.Append(GetATag(pageIndex, GetCommonAHref(pageIndex, pageSize, target, dataType, type), "paginate_button"));
                }

            }
        }


        private static void HandleNextPage(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetATag("<span>&#8594;</span>", GetAHref(currentPageIndex + 1), "next"));
            else
            {
                output.Append(GetATag("<span>&#8594;</span>", "javascript:void(0)", "next"));
            }
        }
        private static void HandleNextCommonPage(StringBuilder output, int totalPagesCount, int currentPageIndex, int pageSize, string target, string dataType, string type)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetATag("下一页", GetCommonAHref(currentPageIndex + 1, pageSize, target, dataType, type), "next paginate_button"));
            else
            {
                output.Append(GetATag("下一页", "javascript:void(0)", "next paginate_button"));
            }
        }


        private static void HandleLastPage(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetATagNext("尾页", GetAHref(totalPagesCount), ""));
            else
            {
                output.Append(GetATagNext("尾页", "javascript:void(0)", ""));
            }
        }
        private static void HandleLastCommonPage(StringBuilder output, int totalPagesCount, int currentPageIndex, int pageSize, string target, string dataType, string type)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetATag("尾页", GetCommonAHref(totalPagesCount, pageSize, target, dataType, type), "last paginate_button"));
            else
            {
                output.Append(GetATag("尾页", "javascript:void(0)", "last paginate_button"));
            }
        }


        private static void HandleTotalSumary(StringBuilder output, int totalCount, int totalPagesCount)
        {
            output.AppendFormat(
                "<span style='float:left;width:auto;color:#565656;margin-right:10px;text-indent: 1em;line-height: 25px;'>共{0}条，共{1}页</span>", totalCount, totalPagesCount);
        }

        private static int GetCurrentPage(HtmlHelper html, int totalPagesCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.Form;
            var currentPageIndex = 0;
            int.TryParse(queryString[REQUEST_PAGEINDEX_NAME], out currentPageIndex);
            if (currentPageIndex == 0) currentPageIndex = 1;
            if (currentPageIndex > totalPagesCount)
                currentPageIndex = totalPagesCount;
            return currentPageIndex;
        }

        private static int GetStartIndex(int totalPagesCount, int currentPage)
        {
            var leftEdge = currentPage - PAGEER_COUNT / 2;
            var startIndex = leftEdge <= 0 ? 1 : leftEdge;
            var rightEdge = startIndex - 1 + PAGEER_COUNT;
            if (totalPagesCount >= PAGEER_COUNT && rightEdge > totalPagesCount) startIndex -= rightEdge - totalPagesCount;

            return startIndex;
        }

        private static string GetATag(int pageIndex, string href, string style)
        {
            //return string.Format("<li class='{2}'><a href='{0}'>{1}</a></li>", href, pageIndex, style);
            return string.Format(" <li class=\"xans-record-\"><a href='{0}' title=\"\">{1}</a></li>", href, pageIndex);
        }

        private static string GetATag(string inner, string href, string style)
        {
            //return string.Format("<li class='{2}'><a href='{0}'>{1}</a></li>", href, inner, style);
            return string.Format("<p><a href='{0}'><img src=\"/Content/upshop/images/btn_pageFirst.gif\" alt=\"\"></a> </p>", href, inner, style);
        }
        private static string GetATagNext(string inner, string href, string style)
        {
            //return string.Format("<li class='{2}'><a href='{0}'>{1}</a></li>", href, inner, style);
            return string.Format(" <p><a href='{0}'><img src=\"/Content/upshop/images/btn_pageLast.gif\" alt=\"\"></a> </p>", href, inner, style);
        }
        private static string GetATag(string inner, string href)
        {
            return string.Format("<a href='{0}'>{1}</a>", href, inner);
        }

        private static string GetAHref(int pageIndex)
        {
            return string.Format(formatPager, pageIndex);
        }
        private static string GetCommonAHref(int pageIndex, int pageSize, string target, string dataType, string type)
        {
            return string.Format(formatPager, pageIndex, pageSize, target, dataType, type);
        }

        
    }
}
